package com.fw.ui.view.settings.usercontrol.parentmerchant;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.fw.service.bean.EntryBean;
import com.fw.service.bean.SearchCriteriaBean;
import com.fw.service.bean.SearchResultBean;
import com.fw.service.parentmerchant.bean.ParentMerchantBean;
import com.fw.service.parentmerchant.bean.SearchParentMerchantBean;
import com.fw.ui.bao.MasterDataBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.ParentMerchantBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.bean.merchantuser.SearchParentMerchantTableItemBean;
import com.fw.ui.core.ViewDescription;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.AbstractView;
import com.fw.ui.view.common.SortingTablePanel;
import com.fw.ui.view.common.TablePanel;
import com.fw.ui.view.common.util.TableHeaderProperty;
import com.fw.ui.view.settings.usercontrol.parentmerchant.event.SearchParentMerchantClearButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.event.SearchParentMerchantNewButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.event.SearchParentMerchantSearchButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.event.SearchParentMerchantTableDeleteButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.event.SearchParentMerchantTableEditButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.event.SearchParentMerchantTablePageButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.event.SearchParentMerchantTableSizeValueChangeListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.event.SearchParentMerchantTableSortButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.event.SearchParentMerchantTableViewButtonClickListener;
import com.fw.util.StringUtils;
import com.vaadin.data.util.BeanItem;
import com.vaadin.shared.ui.combobox.FilteringMode;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table.Align;
import com.vaadin.ui.VerticalLayout;

@org.springframework.stereotype.Component
@Scope(SCOPE_PROTOTYPE)
@ViewDescription(name = "settings-user-control-parentmerchant-search", path = "/settings/user-control/parentmerchant-search")
public class SearchParentMerchantView extends AbstractView {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;

    @Autowired
    private MasterDataBAO masterDataBAO;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private ParentMerchantBAO parentMerchantBAO;

    private Map<String, String> parentStatusMap = new HashMap<String, String>();
    private SearchParentMerchantBean searchParentMerchantBean = new SearchParentMerchantBean();
    private SearchCriteriaBean<SearchParentMerchantBean> searchCriteriaBean = new SearchCriteriaBean<SearchParentMerchantBean>();

    @Autowired
    private SearchParentMerchantClearButtonClickListener searchParentMerchantClearButtonClickListener;
    @Autowired
    private SearchParentMerchantNewButtonClickListener searchParentMerchantNewButtonClickListener;
    @Autowired
    private SearchParentMerchantSearchButtonClickListener searchParentMerchantSearchButtonClickListener;
    @Autowired
    private SearchParentMerchantTableDeleteButtonClickListener searchParentMerchantTableDeleteButtonClickListener;
    @Autowired
    private SearchParentMerchantTableEditButtonClickListener searchParentMerchantTableEditButtonClickListener;
    @Autowired
    private SearchParentMerchantTablePageButtonClickListener searchParentMerchantTablePageButtonClickListener;
    @Autowired
    private SearchParentMerchantTableSizeValueChangeListener searchParentMerchantTableSizeValueChangeListener;
    @Autowired
    private SearchParentMerchantTableSortButtonClickListener searchParentMerchantTableSortButtonClickListener;
    @Autowired
    private SearchParentMerchantTableViewButtonClickListener searchParentMerchantTableViewButtonClickListener;

    /**
     * COMPONENT
     */
    private Panel searchParentMerchantUserPanel = new Panel();

    @Autowired
    protected TablePanel tablePanel;
    @Autowired
    protected SortingTablePanel sortingTablePanel;

    private Label lblParentId = new Label();
    private Label lblParentName = new Label();
    private Label lblParentContactPerson = new Label();
    private Label lblParentStatus = new Label();

    private ComboBox cbbParentId = new ComboBox();
    private ComboBox cbbParentName = new ComboBox();
    private ComboBox cbbParentContactPerson = new ComboBox();
    private ComboBox cbbParentStatus = new ComboBox();

    private Button btnSearch = new Button();
    private Button btnNew = new Button();
    private Button btnClear = new Button();

    private TableHeaderProperty hNo = new TableHeaderProperty();
    private TableHeaderProperty hParentId = new TableHeaderProperty();
    private TableHeaderProperty hParentName = new TableHeaderProperty();
    private TableHeaderProperty hParentContactPerson = new TableHeaderProperty();
    private TableHeaderProperty hParentStatus = new TableHeaderProperty();
    private TableHeaderProperty hCreateDatetime = new TableHeaderProperty();
    private TableHeaderProperty hLastUpdateDatetime = new TableHeaderProperty();
    private TableHeaderProperty hCommand = new TableHeaderProperty();

    private List<Button> tableViewButtons = new ArrayList<Button>();
    private List<Button> tableEditButtons = new ArrayList<Button>();
    private List<Button> tableDeleteButtons = new ArrayList<Button>();

    @Override
    public void updateStrings() {
	this.searchParentMerchantUserPanel.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.title"));
	this.lblParentId.setValue(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.label.parentId"));
	this.lblParentName.setValue(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.label.parentName"));
	this.lblParentContactPerson.setValue(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.label.parentContactPerson"));
	this.lblParentStatus.setValue(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.label.parentStatus"));

	this.cbbParentId.setInputPrompt(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.inputprompt.parentId"));
	this.cbbParentName.setInputPrompt(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.inputprompt.parentName"));
	this.cbbParentContactPerson.setInputPrompt(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.inputprompt.parentContactPerson"));
	this.cbbParentStatus.setInputPrompt(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.inputprompt.parentStatus"));

	this.btnSearch.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.button.search"));
	this.btnNew.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.button.new"));
	this.btnClear.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.button.clear"));

	this.hNo.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.header.no"));
	this.hParentId.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.header.parentId"));
	this.hParentName.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.header.parentName"));
	this.hParentContactPerson.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.header.parentContactPerson"));
	this.hParentStatus.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.header.parentStatus"));
	this.hCreateDatetime.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.header.createDatetime"));
	this.hLastUpdateDatetime.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.header.lastUpdateDatetime"));
	this.hCommand.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.header.command"));

	tablePanel.setTableColumnHeader(this.hNo.getColumnId(), this.hNo.getColumnName());
	tablePanel.setTableColumnHeader(this.hParentId.getColumnId(), this.hParentId.getColumnName());
	tablePanel.setTableColumnHeader(this.hParentName.getColumnId(), this.hParentName.getColumnName());
	tablePanel.setTableColumnHeader(this.hParentContactPerson.getColumnId(), this.hParentContactPerson.getColumnName());
	tablePanel.setTableColumnHeader(this.hParentStatus.getColumnId(), this.hParentStatus.getColumnName());
	tablePanel.setTableColumnHeader(this.hCreateDatetime.getColumnId(), this.hCreateDatetime.getColumnName());
	tablePanel.setTableColumnHeader(this.hLastUpdateDatetime.getColumnId(), this.hLastUpdateDatetime.getColumnName());
	tablePanel.setTableColumnHeader(this.hCommand.getColumnId(), this.hCommand.getColumnName());

	if (tableViewButtons != null && !tableViewButtons.isEmpty()) {
	    for (Button button : tableViewButtons) {
		button.setCaption(this.messageBAO
			.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.button.table.view"));
	    }
	}

	if (tableEditButtons != null && !tableEditButtons.isEmpty()) {
	    for (Button button : tableEditButtons) {
		button.setCaption(this.messageBAO
			.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.button.table.edit"));
	    }
	}

	if (tableDeleteButtons != null && !tableDeleteButtons.isEmpty()) {
	    for (Button button : tableDeleteButtons) {
		button.setCaption(this.messageBAO
			.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.button.table.delete"));
	    }
	}
    }

    @Override
    public void init() {
	this.setSizeFull();

	this.searchParentMerchantUserPanel.setSizeFull();
	this.searchParentMerchantUserPanel.setId("searchParentMerchantPanel");

	lblParentId.setWidth("150px");
	lblParentName.setWidth("150px");
	lblParentContactPerson.setWidth("150px");
	lblParentStatus.setWidth("150px");

	cbbParentId.setWidth("150px");
	cbbParentName.setWidth("150px");
	cbbParentContactPerson.setWidth("150px");
	cbbParentStatus.setWidth("150px");

	this.cbbParentId.setFilteringMode(FilteringMode.STARTSWITH);
	this.cbbParentName.setFilteringMode(FilteringMode.STARTSWITH);
	this.cbbParentContactPerson.setFilteringMode(FilteringMode.STARTSWITH);

	this.cbbParentId.setNewItemsAllowed(true);
	this.cbbParentName.setNewItemsAllowed(true);
	this.cbbParentContactPerson.setNewItemsAllowed(true);

	this.cbbParentStatus.setNullSelectionAllowed(false);
	this.cbbParentStatus.setTextInputAllowed(false);

	this.btnSearch.addStyleName("button-blue");
	this.btnNew.addStyleName("button-blue");
	this.btnClear.addStyleName("button-green");

	this.btnSearch.setDisableOnClick(true);
	this.btnNew.setDisableOnClick(true);
	this.btnClear.setDisableOnClick(true);

	this.btnSearch.setWidth("70px");
	this.btnNew.setWidth("70px");
	this.btnClear.setWidth("70px");

	this.hNo.setColumnId("seqNo");
	this.hNo.setType(Integer.class);
	this.hNo.setAlignment(Align.CENTER);
	this.hNo.setDefaultValue(null);
	this.hNo.setIconResource(null);
	this.hNo.setWidth(50);
	this.hNo.setOrder(1);

	this.hParentId.setColumnId("parentId");
	this.hParentId.setType(String.class);
	this.hParentId.setAlignment(Align.CENTER);
	this.hParentId.setDefaultValue(null);
	this.hParentId.setIconResource(null);
	this.hParentId.setWidth(150);
	this.hParentId.setOrder(2);

	this.hParentName.setColumnId("parentName");
	this.hParentName.setType(String.class);
	this.hParentName.setAlignment(Align.LEFT);
	this.hParentName.setDefaultValue(null);
	this.hParentName.setIconResource(null);
	this.hParentName.setWidth(200);
	this.hParentName.setOrder(3);

	this.hParentContactPerson.setColumnId("parentContactPerson");
	this.hParentContactPerson.setType(String.class);
	this.hParentContactPerson.setAlignment(Align.LEFT);
	this.hParentContactPerson.setDefaultValue(null);
	this.hParentContactPerson.setIconResource(null);
	this.hParentContactPerson.setOrder(4);

	this.hParentStatus.setColumnId("parentStatus");
	this.hParentStatus.setType(String.class);
	this.hParentStatus.setAlignment(Align.CENTER);
	this.hParentStatus.setDefaultValue(null);
	this.hParentStatus.setIconResource(null);
	this.hParentStatus.setWidth(100);
	this.hParentStatus.setOrder(5);

	this.hCreateDatetime.setColumnId("createDatetime");
	this.hCreateDatetime.setType(String.class);
	this.hCreateDatetime.setAlignment(Align.CENTER);
	this.hCreateDatetime.setDefaultValue(null);
	this.hCreateDatetime.setIconResource(null);
	this.hCreateDatetime.setWidth(130);
	this.hCreateDatetime.setOrder(6);

	this.hLastUpdateDatetime.setColumnId("lastUpdateDatetime");
	this.hLastUpdateDatetime.setType(String.class);
	this.hLastUpdateDatetime.setAlignment(Align.CENTER);
	this.hLastUpdateDatetime.setDefaultValue(null);
	this.hLastUpdateDatetime.setIconResource(null);
	this.hLastUpdateDatetime.setWidth(130);
	this.hLastUpdateDatetime.setOrder(7);

	this.hCommand.setColumnId("commandLayout");
	this.hCommand.setType(HorizontalLayout.class);
	this.hCommand.setAlignment(Align.CENTER);
	this.hCommand.setDefaultValue(null);
	this.hCommand.setIconResource(null);
	this.hCommand.setWidth(140);
	this.hCommand.setOrder(8);

	List<TableHeaderProperty> headerProperties = new ArrayList<TableHeaderProperty>();
	headerProperties.add(hNo);
	headerProperties.add(hParentId);
	headerProperties.add(hParentName);
	headerProperties.add(hParentContactPerson);
	headerProperties.add(hParentStatus);
	headerProperties.add(hCreateDatetime);
	headerProperties.add(hLastUpdateDatetime);
	headerProperties.add(hCommand);

	this.tablePanel.setTableBeanContainerColumnHeader(headerProperties, SearchParentMerchantTableItemBean.class);
	tablePanel.setRowIndexVisiblePageNumber();
	tablePanel.setTableSelect(false);
	tablePanel.setAllowSortRecord(false);
	tablePanel.setEnableTableSorting(false);
	tablePanel.setTableColumnReordering(false);
	tablePanel.createControlTablePageNumber();
	this.tablePanel.setSortingTablePanel(this.sortingTablePanel);

	VerticalLayout mainLayout = new VerticalLayout();
	mainLayout.setSizeFull();
	mainLayout.setSizeUndefined();
	mainLayout.setSpacing(true);
	mainLayout.setWidth("100%");

	this.searchParentMerchantUserPanel.setContent(mainLayout);

	VerticalLayout formLayout = new VerticalLayout();
	formLayout.setId("formPanel");
	formLayout.setSizeUndefined();
	formLayout.setWidth("100%");
	formLayout.setSpacing(true);

	HorizontalLayout row0 = new HorizontalLayout();
	HorizontalLayout row1 = new HorizontalLayout();
	HorizontalLayout row2 = new HorizontalLayout();
	HorizontalLayout row3 = new HorizontalLayout();
	HorizontalLayout row4 = new HorizontalLayout();

	row1.setSpacing(true);
	row2.setSpacing(true);
	row3.setSpacing(true);
	row4.setSpacing(true);

	row1.setSizeUndefined();
	row2.setSizeUndefined();
	row3.setSizeUndefined();
	row4.setSizeUndefined();

	HorizontalLayout row1Col1 = new HorizontalLayout();
	HorizontalLayout row1Sep1 = new HorizontalLayout();
	HorizontalLayout row1Col2 = new HorizontalLayout();

	row1Col1.setSpacing(true);
	row1Col2.setSpacing(true);

	row1Sep1.setWidth("25px");

	row1Col1.addComponent(lblParentId);
	row1Col1.addComponent(cbbParentId);
	row1Col2.addComponent(lblParentName);
	row1Col2.addComponent(cbbParentName);

	row1Col1.setComponentAlignment(lblParentId, Alignment.MIDDLE_LEFT);
	row1Col1.setComponentAlignment(cbbParentId, Alignment.MIDDLE_LEFT);
	row1Col2.setComponentAlignment(lblParentName, Alignment.MIDDLE_LEFT);
	row1Col2.setComponentAlignment(cbbParentName, Alignment.MIDDLE_LEFT);

	row1.addComponent(row1Col1);
	row1.addComponent(row1Sep1);
	row1.addComponent(row1Col2);

	HorizontalLayout row2Col1 = new HorizontalLayout();
	HorizontalLayout row2Sep1 = new HorizontalLayout();
	HorizontalLayout row2Col2 = new HorizontalLayout();

	row2Sep1.setWidth("25px");

	row2Col1.setSpacing(true);
	row2Col2.setSpacing(true);

	row2Col1.addComponent(lblParentContactPerson);
	row2Col1.addComponent(cbbParentContactPerson);
	row2Col2.addComponent(lblParentStatus);
	row2Col2.addComponent(cbbParentStatus);

	row2Col1.setComponentAlignment(lblParentContactPerson, Alignment.MIDDLE_LEFT);
	row2Col1.setComponentAlignment(cbbParentContactPerson, Alignment.MIDDLE_LEFT);
	row2Col2.setComponentAlignment(lblParentStatus, Alignment.MIDDLE_LEFT);
	row2Col2.setComponentAlignment(cbbParentStatus, Alignment.MIDDLE_LEFT);

	row2.addComponent(row2Col1);
	row2.addComponent(row2Sep1);
	row2.addComponent(row2Col2);

	row4.addComponent(btnSearch);
	row4.addComponent(btnNew);
	row4.addComponent(btnClear);

	formLayout.addComponent(row0);
	formLayout.addComponent(row1);
	formLayout.addComponent(row2);
	formLayout.addComponent(row3);
	formLayout.addComponent(row4);

	formLayout.setComponentAlignment(row1, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row2, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row3, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row4, Alignment.MIDDLE_CENTER);

	HorizontalLayout tableSortingLayout = new HorizontalLayout();
	tableSortingLayout.setSizeUndefined();
	tableSortingLayout.setWidth("99%");
	tableSortingLayout.addComponent(this.sortingTablePanel);

	HorizontalLayout tableLayout = new HorizontalLayout();
	tableLayout.setSizeUndefined();
	tableLayout.setWidth("99%");
	tableLayout.addComponent(this.tablePanel);

	mainLayout.addComponent(formLayout);
	mainLayout.addComponent(tableSortingLayout);
	mainLayout.addComponent(tableLayout);
	mainLayout.setExpandRatio(tableLayout, 1);

	mainLayout.setComponentAlignment(formLayout, Alignment.MIDDLE_CENTER);
	mainLayout.setComponentAlignment(tableSortingLayout, Alignment.MIDDLE_CENTER);
	mainLayout.setComponentAlignment(tableLayout, Alignment.MIDDLE_CENTER);

	this.addComponent(this.searchParentMerchantUserPanel);
	this.setComponentAlignment(this.searchParentMerchantUserPanel, Alignment.TOP_CENTER);

    }

    @Override
    public void initListener() {
	this.searchParentMerchantClearButtonClickListener.setBasedComponent(this);
	this.searchParentMerchantNewButtonClickListener.setBasedComponent(this);
	this.searchParentMerchantSearchButtonClickListener.setBasedComponent(this);
	this.searchParentMerchantTableDeleteButtonClickListener.setBasedComponent(this);
	this.searchParentMerchantTableEditButtonClickListener.setBasedComponent(this);
	this.searchParentMerchantTablePageButtonClickListener.setBasedComponent(this);
	this.searchParentMerchantTableSizeValueChangeListener.setBasedComponent(this);
	this.searchParentMerchantTableSortButtonClickListener.setBasedComponent(this);
	this.searchParentMerchantTableViewButtonClickListener.setBasedComponent(this);

	this.searchParentMerchantClearButtonClickListener.setAutoEnabled(true);
	this.searchParentMerchantNewButtonClickListener.setAutoEnabled(true);
	this.searchParentMerchantSearchButtonClickListener.setAutoEnabled(true);
	this.searchParentMerchantTableViewButtonClickListener.setAutoEnabled(true);
	this.searchParentMerchantTableDeleteButtonClickListener.setAutoEnabled(true);
	this.searchParentMerchantTableEditButtonClickListener.setAutoEnabled(true);

	this.searchParentMerchantTablePageButtonClickListener.setTablePanel(this.tablePanel);
	this.searchParentMerchantTableSizeValueChangeListener.setTablePanel(this.tablePanel);
	this.searchParentMerchantTableSortButtonClickListener.setTablePanel(this.tablePanel);

	this.tablePanel.addPageTableButtonClickListener(this.searchParentMerchantTablePageButtonClickListener);
	this.tablePanel.addSizeTableValueChangeListener(this.searchParentMerchantTableSizeValueChangeListener);
	this.sortingTablePanel.addSortTableButtonClickListener(this.searchParentMerchantTableSortButtonClickListener);

	this.btnSearch.addClickListener(this.searchParentMerchantSearchButtonClickListener);
	this.btnNew.addClickListener(this.searchParentMerchantNewButtonClickListener);
	this.btnClear.addClickListener(this.searchParentMerchantClearButtonClickListener);
    }

    @Override
    public void initValidator() {

    }

    @SuppressWarnings("rawtypes")
    @Override
    public void initData() {
	try {
	    String sortingDefault = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.default.table.sorting");
	    String sortingItem = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.table.sorting.item");
	    String sortingCaption = this.messageBAO
		    .getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.table.sorting.caption");

	    if (sortingItem != null) {
		String[] sortingItemArr = sortingItem.split(",");
		String[] sortingCaptionArr = sortingCaption.split(",");
		if (sortingItemArr != null) {
		    for (int loop = 0; loop < sortingItemArr.length; loop++) {
			this.sortingTablePanel.addSortingItem(sortingItemArr[loop], sortingCaptionArr[loop]);
		    }
		}
	    }

	    String statusDefault = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.default.status.item");
	    String statusInclude = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.include.status.item");
	    String statusExclude = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.exclude.status.item");
	    String statusIncludeCaption = this.messageBAO
		    .getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.include.status.item.caption");
	    if (statusInclude != null) {
		String[] statusIncludeArr = statusInclude.split(",");
		String[] statusIncludeCaptionArr = statusIncludeCaption.split(",");
		if (statusIncludeArr != null) {
		    for (int loop = 0; loop < statusIncludeArr.length; loop++) {
			this.cbbParentStatus.addItem(statusIncludeArr[loop]);
			this.cbbParentStatus.setItemCaption(statusIncludeArr[loop], statusIncludeCaptionArr[loop]);
		    }
		}
	    }
	    List<String> excludes = new ArrayList<String>();
	    if (statusExclude != null) {
		String[] statusExcludeArr = statusExclude.split(",");
		if (statusExcludeArr != null) {
		    for (int loop = 0; loop < statusExcludeArr.length; loop++) {
			excludes.add(statusExcludeArr[loop]);
		    }
		}
	    }
	    List<EntryBean<String, String>> parentStatus = this.masterDataBAO.selectAllParentMerchantStatus();
	    if (parentStatus != null) {
		for (EntryBean<String, String> entry : parentStatus) {
		    this.parentStatusMap.put(entry.getKey(), entry.getValue());
		    if (!excludes.contains(entry.getKey())) {
			this.cbbParentStatus.addItem(entry.getKey());
			this.cbbParentStatus.setItemCaption(entry.getKey(), entry.getValue());
		    }
		}
	    }
	    this.cbbParentStatus.select(statusDefault);
	    this.cbbParentStatus.setValue(statusDefault);

	    searchCriteriaBean.setDirection1(SearchCriteriaBean.DIRECTION_DESC);
	    searchCriteriaBean.setOrder1(sortingDefault);
	    searchCriteriaBean.setCriteriaBean(searchParentMerchantBean);
	    searchCriteriaBean.setPage(1);
	    searchCriteriaBean.setSize(30);
	    SearchResultBean<ParentMerchantBean> searchResultBean = parentMerchantBAO.selectParentMerchant(searchCriteriaBean);

	    Integer totalPage = searchResultBean.getTotalPages();
	    if (totalPage == null) {
		totalPage = 1;
	    }
	    Long totalElement = searchResultBean.getTotalElements();
	    if (totalElement == null) {
		totalElement = 0l;
	    }
	    List<BeanItem> beanItems = this.generateTableItemBean(searchResultBean);

	    this.tablePanel.clearTableData();
	    this.tablePanel.setManualPageTable(1);
	    this.tablePanel.setTableBeanContainerDataTotalPage(beanItems, totalPage, totalElement);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    @SuppressWarnings("rawtypes")
    public List<BeanItem> generateTableItemBean(SearchResultBean<ParentMerchantBean> searchResultBean) {
	List<BeanItem> beanItems = new ArrayList<BeanItem>();
	if (searchResultBean != null && searchResultBean.getContent() != null) {

	    this.tableViewButtons.clear();
	    this.tableEditButtons.clear();
	    this.tableDeleteButtons.clear();

	    DateFormat dateTimeFormat = new SimpleDateFormat(StringUtils.PATTERN_DATETIME_PERSISTENCE);
	    DateFormat dateTimeFormatUI = new SimpleDateFormat(StringUtils.PATTERN_DATETIME_UI);

	    String activeStatus = propertyBAO
		    .getPropertyValue("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.exclude.status.item.active");

	    for (int loop = 0; loop < searchResultBean.getContent().size(); loop++) {
		ParentMerchantBean parentMerchantBean = searchResultBean.getContent().get(loop);
		SearchParentMerchantTableItemBean searchParentMerchantTableItemBean = new SearchParentMerchantTableItemBean();
		searchParentMerchantTableItemBean.setId(parentMerchantBean.getId());
		searchParentMerchantTableItemBean.setParentId(parentMerchantBean.getParentId());
		searchParentMerchantTableItemBean.setParentName(parentMerchantBean.getParentName());
		String parentContactPerson = "";
		if (parentMerchantBean.getContactPersonFirstName() != null) {
		    parentContactPerson += " " + parentMerchantBean.getContactPersonFirstName();
		}
		if (parentMerchantBean.getContactPersonLastName() != null) {
		    parentContactPerson += " " + parentMerchantBean.getContactPersonLastName();
		}
		// if (parentMerchantBean.getContactPersonEmailAddress() != null) {
		// parentContactPerson += " " + parentMerchantBean.getContactPersonEmailAddress();
		// }
		searchParentMerchantTableItemBean.setParentContactPerson(parentContactPerson.trim());

		String parentStatusText = this.parentStatusMap.get(parentMerchantBean.getParentStatus());
		if (parentStatusText == null) {
		    parentStatusText = parentMerchantBean.getParentStatus();
		}
		searchParentMerchantTableItemBean.setParentStatus(parentStatusText);

		if (parentMerchantBean.getCreateDatetime() != null) {
		    try {
			Date date = dateTimeFormat.parse(parentMerchantBean.getCreateDatetime());
			String strUI = dateTimeFormatUI.format(date);
			searchParentMerchantTableItemBean.setCreateDatetime(strUI);
		    } catch (Exception e) {
		    }
		}
		if (parentMerchantBean.getLastUpdateDatetime() != null) {
		    try {
			Date date = dateTimeFormat.parse(parentMerchantBean.getLastUpdateDatetime());
			String strUI = dateTimeFormatUI.format(date);
			searchParentMerchantTableItemBean.setLastUpdateDatetime(strUI);
		    } catch (Exception e) {
		    }
		}

		HorizontalLayout hLayout = new HorizontalLayout();
		Button btnView = new Button(
			this.messageBAO
				.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.button.table.view"));
		Button btnEdit = new Button(
			this.messageBAO
				.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.button.table.edit"));
		Button btnDelete = new Button(
			this.messageBAO
				.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.SearchParentMerchantView.button.table.delete"));

		btnView.setData(parentMerchantBean);
		btnEdit.setData(parentMerchantBean);
		btnDelete.setData(parentMerchantBean);

		btnView.addClickListener(this.searchParentMerchantTableViewButtonClickListener);
		btnEdit.addClickListener(this.searchParentMerchantTableEditButtonClickListener);
		btnDelete.addClickListener(this.searchParentMerchantTableDeleteButtonClickListener);

		btnView.setDisableOnClick(true);
		btnEdit.setDisableOnClick(true);
		btnDelete.setDisableOnClick(true);

		btnView.addStyleName("button-green-small");
		btnEdit.addStyleName("button-blue-small");
		btnDelete.addStyleName("button-red-small");

		if (!activeStatus.equals(parentMerchantBean.getParentStatus())) {
		    btnDelete.setEnabled(false);
		}

		tableViewButtons.add(btnView);
		tableEditButtons.add(btnEdit);
		tableDeleteButtons.add(btnDelete);

		hLayout.addComponent(btnView);
		hLayout.addComponent(new Label("&nbsp;", ContentMode.HTML));
		hLayout.addComponent(btnEdit);
		hLayout.addComponent(new Label("&nbsp;", ContentMode.HTML));
		hLayout.addComponent(btnDelete);

		searchParentMerchantTableItemBean.setCommandLayout(hLayout);

		beanItems.add(new BeanItem<SearchParentMerchantTableItemBean>(searchParentMerchantTableItemBean));
	    }
	}
	return beanItems;
    }

    public MasterDataBAO getMasterDataBAO() {
	return masterDataBAO;
    }

    public MessageBAO getMessageBAO() {
	return messageBAO;
    }

    public PropertyBAO getPropertyBAO() {
	return propertyBAO;
    }

    public ParentMerchantBAO getParentMerchantBAO() {
	return parentMerchantBAO;
    }

    public Map<String, String> getParentStatusMap() {
	return parentStatusMap;
    }

    public SearchParentMerchantBean getSearchParentMerchantBean() {
	return searchParentMerchantBean;
    }

    public SearchCriteriaBean<SearchParentMerchantBean> getSearchCriteriaBean() {
	return searchCriteriaBean;
    }

    public SearchParentMerchantClearButtonClickListener getSearchParentMerchantClearButtonClickListener() {
	return searchParentMerchantClearButtonClickListener;
    }

    public SearchParentMerchantNewButtonClickListener getSearchParentMerchantNewButtonClickListener() {
	return searchParentMerchantNewButtonClickListener;
    }

    public SearchParentMerchantSearchButtonClickListener getSearchParentMerchantSearchButtonClickListener() {
	return searchParentMerchantSearchButtonClickListener;
    }

    public SearchParentMerchantTableDeleteButtonClickListener getSearchParentMerchantTableDeleteButtonClickListener() {
	return searchParentMerchantTableDeleteButtonClickListener;
    }

    public SearchParentMerchantTableEditButtonClickListener getSearchParentMerchantTableEditButtonClickListener() {
	return searchParentMerchantTableEditButtonClickListener;
    }

    public SearchParentMerchantTablePageButtonClickListener getSearchParentMerchantTablePageButtonClickListener() {
	return searchParentMerchantTablePageButtonClickListener;
    }

    public SearchParentMerchantTableSizeValueChangeListener getSearchParentMerchantTableSizeValueChangeListener() {
	return searchParentMerchantTableSizeValueChangeListener;
    }

    public SearchParentMerchantTableSortButtonClickListener getSearchParentMerchantTableSortButtonClickListener() {
	return searchParentMerchantTableSortButtonClickListener;
    }

    public SearchParentMerchantTableViewButtonClickListener getSearchParentMerchantTableViewButtonClickListener() {
	return searchParentMerchantTableViewButtonClickListener;
    }

    public Panel getSearchParentMerchantUserPanel() {
	return searchParentMerchantUserPanel;
    }

    public TablePanel getTablePanel() {
	return tablePanel;
    }

    public SortingTablePanel getSortingTablePanel() {
	return sortingTablePanel;
    }

    public Label getLblParentId() {
	return lblParentId;
    }

    public Label getLblParentName() {
	return lblParentName;
    }

    public Label getLblParentContactPerson() {
	return lblParentContactPerson;
    }

    public Label getLblParentStatus() {
	return lblParentStatus;
    }

    public ComboBox getCbbParentId() {
	return cbbParentId;
    }

    public ComboBox getCbbParentName() {
	return cbbParentName;
    }

    public ComboBox getCbbParentContactPerson() {
	return cbbParentContactPerson;
    }

    public ComboBox getCbbParentStatus() {
	return cbbParentStatus;
    }

    public Button getBtnSearch() {
	return btnSearch;
    }

    public Button getBtnNew() {
	return btnNew;
    }

    public Button getBtnClear() {
	return btnClear;
    }

    public TableHeaderProperty gethNo() {
	return hNo;
    }

    public TableHeaderProperty gethParentId() {
	return hParentId;
    }

    public TableHeaderProperty gethParentName() {
	return hParentName;
    }

    public TableHeaderProperty gethParentContactPerson() {
	return hParentContactPerson;
    }

    public TableHeaderProperty gethParentStatus() {
	return hParentStatus;
    }

    public TableHeaderProperty gethCommand() {
	return hCommand;
    }

    public List<Button> getTableViewButtons() {
	return tableViewButtons;
    }

    public List<Button> getTableEditButtons() {
	return tableEditButtons;
    }

    public List<Button> getTableDeleteButtons() {
	return tableDeleteButtons;
    }

    public void setSearchCriteriaBean(SearchCriteriaBean<SearchParentMerchantBean> searchCriteriaBean) {
	this.searchCriteriaBean = searchCriteriaBean;
    }

    public TableHeaderProperty gethLastUpdateDatetime() {
	return hLastUpdateDatetime;
    }

}
